package io.tan.searchestest.controller;

import com.alibaba.fastjson.JSONObject;
import com.detabes.search.es.dto.EqDTO;
import com.detabes.search.es.dto.SortDTO;
import com.detabes.search.es.util.EsIndexUtil;
import com.detabes.search.es.util.EsSearchUtil;
import com.detabes.search.es.vo.EsPageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * ElasticSearch控制器
 *
 * @author 154594742@qq.com
 * @date 2021/3/5 10:02
 */

@RestController
@RequestMapping("elasticSearch")
public class ElasticSearchController {

	private EsSearchUtil esSearchUtil;

	private EsIndexUtil esIndexUtil;

	public ElasticSearchController(EsSearchUtil esSearchUtil, EsIndexUtil esIndexUtil) {
		this.esSearchUtil = esSearchUtil;
		this.esIndexUtil = esIndexUtil;
	}

	@PostMapping("search")
	public EsPageVO search(String index, Integer startPage, Integer pageSize) throws IOException {

		return esSearchUtil.getSearch(Arrays.asList(index.split(",")), startPage, pageSize);
	}

	@PostMapping("search1")
	public EsPageVO search1(String index, String dwdm, String tableId, String term, String highlightField,
							String fields, String sunFields, Integer startPage, Integer pageSize,
							String orderField, Integer orderType) throws IOException {
		List<SortDTO> sortDTOS = new ArrayList<>();
		SortDTO sortDTO = new SortDTO();
		sortDTO.setOrderBy(orderField);
		sortDTO.setOrderDesc(orderType);
		sortDTOS.add(sortDTO);
		List<EqDTO> list1 = new ArrayList<>();
		EqDTO dto = new EqDTO();
		dto.setField("DWDM");
		dto.setFieldValue(StringUtils.isNotBlank(dwdm) ? Arrays.asList(term.split(",")) : null);
		list1.add(dto);
		dto.setField("tableId");
		dto.setFieldValue(StringUtils.isNotBlank(tableId) ? Arrays.asList(term.split(",")) : null);
		list1.add(dto);

		return esSearchUtil.getSearch1(Arrays.asList(index.split(","))
				, list1
				, StringUtils.isNotBlank(term) ? Arrays.asList(term.split(" ")) : null
				, StringUtils.isNotBlank(fields) ? Arrays.asList(fields.split(",")) : null
				, "fubiao"
				, StringUtils.isNotBlank(sunFields) ? Arrays.asList(sunFields.split(",")) : null
				, null, null, null
				, highlightField
				, startPage, pageSize, sortDTOS);
	}

	/**
	 * 新增索引映射
	 *
	 * @param index  索引
	 * @param fields 映射字段
	 * @return ResponseVo
	 */
	@PostMapping("createIndexMapping")
	public boolean createIndexMapping(String index, String fields, String sunFields) throws Exception {
		List<String> str1 = Arrays.asList(fields.split(","));
		List<String> str2 = Arrays.asList(sunFields.split(","));
		return esIndexUtil.createIndexMapping(index, str1, "fubiao", str2);
	}

	/**
	 * 新增索引
	 *
	 * @param index 索引
	 * @return ResponseVo
	 */
	@PostMapping("createIndex")
	public boolean createIndex(String index) throws IOException {
		return esIndexUtil.createIndex(index);
	}

	/**
	 * 索引是否存在
	 *
	 * @param index index
	 * @return ResponseVo
	 */
	@GetMapping("index/{index}")
	public boolean existIndex(@PathVariable String index) throws IOException {
		return esIndexUtil.isIndexExist(index);
	}

	/**
	 * 删除索引
	 *
	 * @param index index
	 * @return ResponseVo
	 */
	@PostMapping("deleteIndex")
	public boolean deleteIndex(String index) throws IOException {
		return esIndexUtil.deleteIndex(index);
	}

	/**
	 * 新增/更新数据
	 *
	 * @param entity 数据
	 * @param index  索引
	 * @param esId   esId
	 * @return ResponseVo
	 */
	@PostMapping("submitData")
	public String submitData(String entity, String index, String esId) throws IOException {
		return esIndexUtil.submitData(JSONObject.parseObject(entity), index, esId);
	}

	/**
	 * 通过ID 更新数据,保证实时性
	 *
	 * @param entity 更新的数据
	 * @param index  索引，类似数据库
	 * @param esId   索引数据ID
	 * @return string
	 */
	@PostMapping("updateDataByIdNoRealTime")
	public String updateDataByIdNoRealTime(String entity, String index, String esId) throws IOException {

		return esIndexUtil.updateDataByIdNoRealTime(JSONObject.parseObject(entity), index, esId);
	}

	/**
	 * 通过id删除数据
	 *
	 * @param index index
	 * @param id    id
	 * @return ResponseVo
	 */
	@PostMapping("deleteDataById")
	public String deleteDataById(String index, String id) throws IOException {
		return esIndexUtil.deleteDataById(index, id);
	}

	/**
	 * 通过id查询数据
	 *
	 * @param index  index
	 * @param id     id
	 * @param fields 需要查询的字段，逗号分隔（缺省为全部字段）
	 * @return ResponseVo
	 */
	@GetMapping("searchDataById")
	public Map<String, Object> searchDataById(String index, String id, String fields) throws IOException {
		return esIndexUtil.searchDataById(index, id, fields);
	}

}
